package judesart.app.test.remote.utils;

import java.io.IOException;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;

public class RemoteApiUtils {
	
	private void loadData(){
		try {
			RemoteApiOptions options = new RemoteApiOptions().
					server("localhost",8080).
					credentials("test@example.com", "");
			RemoteApiInstaller installer = new RemoteApiInstaller();
			installer.install(options);
			try {
				DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
				Key key = KeyFactory.createKey("Artwork", 5136918324969472L);
				Entity entity = ds.get(key);
				Object description = entity.getProperty("description");
				System.out.println("description [" + description + "]");
				System.out.println("adding...");
				for (long i = 3; i < 100; i++) {
					Entity newEnt = new Entity("Artwork", i);
					newEnt.setPropertiesFrom(entity);
					
					ds.put(newEnt);
					System.out.print(".");
				}
				System.out.println("done.");
			} finally {
				installer.uninstall();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void updateArtworsorksFeaturedLive(){
		try {
			RemoteApiOptions options = new RemoteApiOptions().
					server("judeshayart-hrd.appspot.com",443).
					credentials("pedrof.costa@gmail.com", "pypLWkvp^");
			RemoteApiInstaller installer = new RemoteApiInstaller();
			installer.install(options);
			try {
				DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
			    //Transaction tx = ds.beginTransaction();
				//Filter filter = new Query.FilterPredicate("featured", FilterOperator.NOT_EQUAL, new Boolean(true));
				Query q = new Query("Artwork");
				
				PreparedQuery pq = ds.prepare(q);
				System.out.print("\nQuering...\n");
				for(Entity ent : pq.asIterable() ){
					System.out.println("[" + ent.getKey().getId() + "] - [" + ent.getProperty("featured") + "]");
					if (ent.getProperty("featured") == null){
						ent.setProperty("featured", new Boolean (false));
						ds.put(ent);
						System.out.print("updated!");
					}
				}
//				tx.commit();
				System.out.println("done!");
			} finally {
				installer.uninstall();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		RemoteApiUtils utils = new RemoteApiUtils();
//		utils.loadData();
		utils.updateArtworsorksFeaturedLive();
	}

}
